const nunjucks = require('nunjucks'),
path=require('path');

let nunjucksEVN = new nunjucks.Environment(new nunjucks.FileSystemLoader());

// 判断文件是否有html后缀
let isHtmlReg = /\.html$/;
let resolvePath = (params={},filePath)=>{
    filePath = isHtmlReg.test(filePath)?filePath:filePath+(params.suffix || '.html');
    return path.resolve(params.path || '' , filePath);
};

module.exports = (params)=>{
      return (ctx,next)=>{
          ctx.render = (filePath,renderData)=>{
              ctx.type = "text/html";
              ctx.body = nunjucksEVN.render(resolvePath(params,filePath),Object.assign(ctx.state,renderData));
          }
          return next();
      }
}